package com.sgb.kjwl.view.ui.activity.transport;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCargoQuotationSuccessBinding;
import com.sgb.kjwl.view.constant.RouterKey;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.LogisticsEvent;
import com.sgb.kjwl.view.ui.activity.self.carMaster.MyOrderDetailsActivity;

import org.greenrobot.eventbus.EventBus;

/**
 * @author: 熊丽
 * @date: 2021/8/30 15:50
 * @Description: 竞价-接单成功页面
 */
public class BiddingQuotationSuccessActivity extends AppActivity {

    ActivityCargoQuotationSuccessBinding mBinding;
    //是否是我的报价管理进入(区分报价成功页展示)
    private boolean mIsManager;
    private String offerNo;

    public static void start(Context context, boolean isManager) {
        Intent intent = new Intent(context, BiddingQuotationSuccessActivity.class);
        intent.putExtra(RouterKey.BOOLEAN, isManager);
        context.startActivity(intent);
    }

    public static void start(Context context, boolean isManager, String offerNo) {
        Intent intent = new Intent(context, BiddingQuotationSuccessActivity.class);
        intent.putExtra(RouterKey.BOOLEAN, isManager);
        intent.putExtra(RouterKey.NO, offerNo);
        context.startActivity(intent);
    }

    public static void startForResult(Activity activity, boolean isManager, String offerNo) {
        Intent intent = new Intent(activity, BiddingQuotationSuccessActivity.class);
        intent.putExtra(RouterKey.BOOLEAN, isManager);
        intent.putExtra(RouterKey.NO, offerNo);
        activity.startActivityForResult(intent, CargoQuotationActivity.REQUEST_CODE);
    }

    @Override
    protected void init() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_cargo_quotation_success);
        mBinding.setLifecycleOwner(this);
        mBinding.includeTitle.titleView.setText("接单结果");
        mBinding.tvSuccess.setText("接单成功");
        mBinding.tvCargoQuotationSuccessToDetail.setText("查看接单详情");
        mBinding.quotationTips.setText("请耐心等待货主处理您的接单请求，货主同意后将产生竞价订单");
        mBinding.setOnClick(this);
        initData();
        initOnClick();
    }


    private void initData() {
        mIsManager = getIntent().getBooleanExtra(RouterKey.BOOLEAN, false);
        if (mIsManager) {
            mBinding.tvCargoQuotationSuccessBtn.setText("返回");
            mBinding.tvCargoQuotationSuccessBtn.setVisibility(View.VISIBLE);
            mBinding.tvCargoQuotationSuccessToDetail.setVisibility(View.GONE);
        } else {
            mBinding.tvCargoQuotationSuccessBtn.setVisibility(View.GONE);//2.1.5 去掉返回列表功能
            mBinding.tvCargoQuotationSuccessToDetail.setVisibility(View.VISIBLE);
        }
        offerNo = getIntent().getStringExtra(RouterKey.NO);
    }

    private void initOnClick() {
        mBinding.includeTitle.ivBack.setOnClickListener(this);
        mBinding.tvCargoQuotationSuccessBtn.setOnClickListener(this);
        mBinding.tvCargoQuotationSuccessToDetail.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        if (v == mBinding.includeTitle.ivBack) {
            successOfferCloseActivity();
        } else if (v == mBinding.tvCargoQuotationSuccessBtn) {
            //2.1.5 去掉返回列表功能
//            if ("返回列表".equals(mBinding.tvCargoQuotationSuccessBtn.getText())){
//                successCloseListActivity();
//            }else {
            successOfferCloseActivity();
//            }
        } else if (v == mBinding.tvCargoQuotationSuccessToDetail) {
            if (!TextUtils.isEmpty(offerNo)) {
                successOfferCloseActivity();
                MyOrderDetailsActivity.start(getActivity(), offerNo);
            } else {
                ToastUtil.show(getActivity(), "获取详情失败");
            }
        }
    }

    /**
     * 报价成功后关闭页面，提醒相关页面刷新状态
     */
    private void successOfferCloseActivity() {
        EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_MY_OFFER_PAGE));
        Intent intent = new Intent();
        intent.putExtra("no", offerNo);
        setResult(PayBiddingOrderActivity.RESULT_CODE, intent);
        finish();
    }

    /**
     * 接单成功后关闭页面，返回列表
     */
    private void successCloseListActivity() {
        EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_MY_OFFER_PAGE).setMessageData("ture"));
        Intent intent = new Intent();
        intent.putExtra("no", offerNo);
        setResult(PayBiddingOrderActivity.RESULT_CODE, intent);
        finish();
    }
}
